MEGpipeline_RunICA.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. % Performs ICA decomposition on MEG data read into FieldTrip. %
  3. % Last modified: July 18, 2014 %
  4. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  5. % Copyright (C) 2013-2014, Michael J. Cheung
  6. %
  7. % This file is a part of the MEG & PLS Pipeline (MEGPLS). For more
  8. % details, see the documentation included with the software package.
  9. %
  10. % MEGPLS is free software: you can redistribute it and/or modify it under
  11. % the terms of the GNU General Public License version 2 as published by
  12. % the Free Software Foundation. This program is distributed in the hope
  13. % that it will be useful, but WITHOUT ANY WARRANTY; without even the
  14. % implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. % See the GNU General Public License for more details.
  16. %
  17. % You should have received a copy of the GNU General Public License along
  18. % with this program. If not, you can download the license here:
  19. % <http://www.gnu.org/licenses/old-licenses/gpl-2.0>.
  20. function MEGpipeline_RunICA(FTcfg, InputParams, RunChoice)
  21. % Make sure java paths added:
  22. UseProgBar = CheckParforJavaPaths;
  23. % Load InputParams:
  24. name = InputParams.name;
  25. paths = InputParams.paths;
  26. % Set up errorlog and diary:
  27. if exist('ErrorLog_ICA.txt', 'file')
  28. system('rm ErrorLog_ICA.txt');
  29. end
  30. if exist('Diary_ICA.txt', 'file')
  31. system('rm Diary_ICA.txt');
  32. end
  33. diary Diary_ICA.txt
  34. ErrLog = fopen('ErrorLog_ICA.txt', 'a');
  35. %==========================%
  36. % RESAMPLE DATA & RUN ICA: %
  37. %==========================%
  38. for g = 1:length(name.GroupID)
  39. NumSubj = length(name.SubjID{g});
  40. if UseProgBar == 1
  41. ppm = ParforProgMon...
  42. (['RUNNING ICA DECOMPOSITION: ',name.GroupID{g},'. '], NumSubj, 1, 700, 80);
  43. end
  44. parfor s = 1:length(name.SubjID{g})
  45. for c = 1:length(name.CondID)
  46. % Check input dataset:
  47. CheckInput = CheckPipelineMat(paths.InputPreprocMEG{g}{s,c}, 'ICA');
  48. if CheckInput == 0
  49. continue;
  50. end
  51. % Resample MEG data for ICA:
  52. cfgResampleICA = [];
  53. cfgResampleICA = FTcfg.ResampleICA;
  54. cfgResampleICA.inputfile = paths.InputPreprocMEG{g}{s,c}; % Read input data
  55. ResampledData = ft_resampledata(cfgResampleICA);
  56. % Run ICA decomposition:
  57. cfgICA = [];
  58. cfgICA = FTcfg.ICA;
  59. if exist(paths.ICAunmixing{g}{s,c}, 'file') && strcmp(RunChoice, 'APPLY')
  60. UnmixingInfo = LoadFTmat(paths.ICAunmixing{g}{s,c}, 'ICA');
  61. if isempty(UnmixingInfo)
  62. continue;
  63. end
  64. cfgICA.unmixing = UnmixingInfo.unmixing;
  65. cfgICA.topolabel = UnmixingInfo.topolabel;
  66. elseif strcmp(RunChoice, 'REDO')
  67. if isfield(cfgICA, 'unmixing')
  68. cfgICA = rmfield(cfgICA, 'unmixing');
  69. end
  70. if isfield(cfgICA, 'topolabel')
  71. cfgICA = rmfield(cfgICA, 'topolabel');
  72. end
  73. end
  74. ICAcomponents = ft_componentanalysis(cfgICA, ResampledData);
  75. % Save ICA components, unmixing info, and call-info:
  76. CheckSavePath(paths.ICAunmixing{g}{s,c}, 'ICA');
  77. CheckSavePath(paths.ICAcomponents{g}{s,c}, 'ICA');
  78. ParforSaveICA...
  79. (paths.ICAcomponents{g}{s,c}, paths.ICAunmixing{g}{s,c}, ICAcomponents, cfgICA);
  80. ResampledData = []; % Free memory immediately
  81. ICAcomponents = [];
  82. end % Cond
  83. if UseProgBar == 1 && mod(s, 1) == 0
  84. ppm.increment(); % move up progress bar
  85. end
  86. end % Subj
  87. if UseProgBar == 1
  88. ppm.delete();
  89. end
  90. end % Group
  91. %=========================%
  92. % CHECK FOR OUTPUT FILES: %
  93. %=========================%
  94. for g = 1:length(name.GroupID)
  95. for s = 1:length(name.SubjID{g})
  96. for c = 1:length(name.CondID)
  97. if ~exist(paths.ICAunmixing{g}{s,c}, 'file')
  98. fprintf(ErrLog, ['ERROR: Missing ICA unmixing info file for:'...
  99. '\n %s \n\n'], paths.ICAunmixing{g}{s,c});
  100. end
  101. if ~exist(paths.ICAcomponents{g}{s,c}, 'file')
  102. fprintf(ErrLog, ['ERROR: Missing ICA component results file for:'...
  103. '\n %s \n\n'], paths.ICAcomponents{g}{s,c});
  104. end
  105. end
  106. end
  107. end
  108. %=================%
  109. if exist([pwd,'/ErrorLog_ICA.txt'], 'file')
  110. LogCheck = dir('ErrorLog_ICA.txt');
  111. if LogCheck.bytes ~= 0 % File not empty
  112. open('ErrorLog_ICA.txt');
  113. else
  114. delete('ErrorLog_ICA.txt');
  115. end
  116. end
  117. fclose(ErrLog);
  118. diary off
  119. end
  120. function ParforSaveICA(OutpathComp, OutpathUnmix, ICAcomponents, cfgICA)
  121. % Save ICA unmixing info separately as well:
  122. ICAunmixing.ICAmethod = ICAcomponents.cfg.method;
  123. ICAunmixing.fsample = ICAcomponents.fsample;
  124. ICAunmixing.unmixing = ICAcomponents.unmixing;
  125. ICAunmixing.topolabel = ICAcomponents.topolabel;
  126. ICAunmixing.label = ICAcomponents.label;
  127. ICAunmixing.cfgICA = cfgICA;
  128. ICAcomponents
  129. ICAunmixing
  130. save(OutpathUnmix, 'ICAunmixing');
  131. save(OutpathComp, 'ICAcomponents');
  132. end